package com.letv.core.download.image;

import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.LruCache;

import java.lang.ref.SoftReference;
import java.util.HashMap;

/**
 * lru缓存
 * 
 * @author user
 * 
 */
public class LruMemoryCache implements IMemoryCache {
    private final LruCache<String, Bitmap> mLruCache;
    private final HashMap<String, SoftReference<Bitmap>> mSoftCache;

//    public LruMemoryCache() {
//        mSoftCache = new HashMap<String, SoftReference<Bitmap>>();
//
//        final int memoryClass = ((ActivityManager) BaseApplication.getInstance().getSystemService(
//                Context.ACTIVITY_SERVICE)).getMemoryClass();
//        final int memorySize = memoryClass * 1024 * 1024 / 8;
//        mLruCache = new LruCache<String, Bitmap>(memorySize) {
//
//            // 必须重写此方法，来测量Bitmap的大小
//            @Override
//            protected int sizeOf(String key, Bitmap value) {
//                return value.getRowBytes() * value.getHeight();
//            }
//
//            @Override
//            protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) {
//                // 当LruCache的内存容量满的时候会调用,将oldValue的元素移除出来腾出空间给新的元素加入
//                if (!TextUtils.isEmpty(key) && oldValue != null && !oldValue.isRecycled()) {
//                    mSoftCache.put(key, new SoftReference<Bitmap>(oldValue));
//                }
//            }
//
//        };
//    }

    public LruMemoryCache(Context context) {
        mSoftCache = new HashMap<String, SoftReference<Bitmap>>();

        final int memoryClass = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
        final int memorySize = memoryClass * 1024 * 1024 / 8;
        mLruCache = new LruCache<String, Bitmap>(memorySize) {

            // 必须重写此方法，来测量Bitmap的大小
            @Override
            protected int sizeOf(String key, Bitmap value) {
                return value.getRowBytes() * value.getHeight();
            }

            @Override
            protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) {
                // 当LruCache的内存容量满的时候会调用,将oldValue的元素移除出来腾出空间给新的元素加入
                if (!TextUtils.isEmpty(key) && oldValue != null && !oldValue.isRecycled()) {
                    mSoftCache.put(key, new SoftReference<Bitmap>(oldValue));
                }
            }

        };
    }

    @Override
    public void put(String key, Bitmap value) {
        if (TextUtils.isEmpty(key) || value == null) {
            return;
        }

        mLruCache.put(key, value);
    }

    @Override
    public Bitmap get(String key) {
        if (TextUtils.isEmpty(key)) {
            return null;
        }

        Bitmap bitmap = mLruCache.get(key);
        if (bitmap != null) {
            return bitmap;
        }

        SoftReference<Bitmap> reference = mSoftCache.get(key);
        if (reference != null) {
            return reference.get();
        }
        return null;
    }

    @Override
    public void remove(String key) {
        if (TextUtils.isEmpty(key)) {
            return;
        }

        mLruCache.remove(key);
        mSoftCache.remove(key);
    }

    @Override
    public void clear() {
        mLruCache.evictAll();
        mSoftCache.clear();
//        BaseApplication.callGc();
    }

}
